package com.beiing.tianshuai.tianshuai.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.beiing.tianshuai.tianshuai.R;
import com.beiing.tianshuai.tianshuai.app.HttpRequestConstant;
import com.beiing.tianshuai.tianshuai.entity.DynamicCommentNoticeBean;
import com.beiing.tianshuai.tianshuai.util.DateUtil;
import com.beiing.tianshuai.tianshuai.util.SpanStringUtils;
import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.request.RequestOptions;
import com.qmuiteam.qmui.widget.QMUIRadiusImageView;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by magikliu on 2017/9/28.
 */

public class CommentNotice_RVAdapter extends RecyclerView.Adapter<CommentNotice_RVAdapter.mViewHolder> {

    private Context mContext;
    private List<DynamicCommentNoticeBean.DataBean.NumberBean> mData;
    private LayoutInflater layoutInflater;
    private RequestOptions requestOptions;
    private OnItemClickListener mOnItemClickListener;
    private OnAvatarClickListener mOnAvatarClicKListener;

    public CommentNotice_RVAdapter(Context mContext, List<DynamicCommentNoticeBean.DataBean.NumberBean> mData) {
        this.mContext = mContext;
        this.mData = mData;
        layoutInflater = LayoutInflater.from(mContext);
        requestOptions = new RequestOptions()
                .diskCacheStrategy(DiskCacheStrategy.ALL)
                .skipMemoryCache(false)
                .centerCrop()
                .override(350, 350);
    }

    @Override
    public mViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = layoutInflater.inflate(R.layout.item_notice_comment, parent, false);
        return new mViewHolder(view);
    }

    @Override
    public void onBindViewHolder(final mViewHolder holder, final int position) {
        /*  显示部分  */
        Glide.with(mContext)
                .load(TextUtils.isEmpty(mData.get(position).getPhoto()) ? R.mipmap.default_avatar
                        : HttpRequestConstant.USER_PIC_HEAD + mData.get(position).getPhoto())
                .apply(requestOptions)
                .into(holder.civAvatarComment);
        holder.tvName.setText(mData.get(position).getName());
        holder.tvTime.setText(DateUtil.getTime(mData.get(position).getCreateTime()));
        try {
            String commentContentDecode = URLDecoder.decode(mData.get(position).getCommentContent(), "UTF-8");
            holder.tvCommentContent.setText(SpanStringUtils.getEmotionContent(mContext, holder.tvCommentContent, commentContentDecode));
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        if (TextUtils.isEmpty(mData.get(position).getPicname())) {
            holder.ivDynamicPic.setVisibility(View.GONE);
        } else {
            holder.ivDynamicPic.setVisibility(View.VISIBLE);
            Glide.with(mContext)
                    .asBitmap()
                    .load(HttpRequestConstant.DYNAMIC_PIC_HEAD + mData.get(position).getPicname())
                    .apply(requestOptions)
                    .into(holder.ivDynamicPic);
        }

        //头像点击事件
        holder.civAvatarComment.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (mOnAvatarClicKListener != null){
                    mOnAvatarClicKListener.onAvatarClick(view, holder.getAdapterPosition());
                }
            }
        });

        //条目点击事件
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (mOnItemClickListener != null){
                    mOnItemClickListener.onItemClick(view, holder.getAdapterPosition());
                }
            }
        });
    }

    @Override
    public int getItemCount() {
        return mData == null ? 0 : mData.size();
    }

    class mViewHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.civ_avatar_comment)
        QMUIRadiusImageView civAvatarComment;
        @BindView(R.id.tv_name)
        TextView tvName;
        @BindView(R.id.tv_time)
        TextView tvTime;
        @BindView(R.id.tv_comment_content)
        TextView tvCommentContent;
        @BindView(R.id.iv_dynamic_pic)
        QMUIRadiusImageView ivDynamicPic;

        public mViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }

    public interface OnItemClickListener{
        void onItemClick(View view, int position);
    }

    public void setOnItemClickListener(OnItemClickListener listener){
        this.mOnItemClickListener = listener;
    }

    public interface OnAvatarClickListener{
        void onAvatarClick(View view, int position);
    }

    public void setOnAvatarClickListener(OnAvatarClickListener listener){
        this.mOnAvatarClicKListener = listener;
    }
}
